package com.gitee.feizns.quickstart.ding.talk.api.v10.oauth2;

import com.gitee.feizns.quickstart.ding.talk.Config;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 服务端API > 获取访问凭证
 *
 * /v1.0/oauth2
 *
 * @link <a href="https://open.dingtalk.com/document/orgapp/authorization-overview">服务端API > 获取访问凭证</a>
 * @author feizns
 * @since 2022/11/9
 */
@Slf4j
@RequiredArgsConstructor
public class OAuth2 {

    /**
     * {@link Config}
     */
    @Getter
    private final Config config;

    /**
     * {@link AccessToken}
     */
    private volatile AccessToken accessToken;

    /**
     * 取应用身份相关访问凭证 > 获取企业内部应用的accessToken
     *
     * /v1.0/oauth2/accessToken
     *
     * 服务端API获取访问凭证获取应用身份相关访问凭证获取企业内部应用的accessToken
     * @link <a href="https://open.dingtalk.com/document/orgapp-server/obtain-the-access_token-of-an-internal-app">获取企业内部应用的accessToken</a>
     * @return {@link AccessToken}
     */
    public String accessToken() {
        if ( accessToken == null ) {
            synchronized (this) {
                if ( accessToken == null )
                    accessToken = new AccessToken(config);
            }
        }
        return accessToken.get();
    }

}
